package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SkuSkillRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @GetMapping("/kill")
    public String kill(@RequestParam("skuId")String skuId,
                       @RequestParam("num")Integer num,
                       @RequestParam("key")String key,
                       Model model){
        //判断是否登录(拦截器完成)
        String orderSn = seckillService.kill(skuId,num,key);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
    @GetMapping("/SkuskillLatest")
    @ResponseBody
    public R querySkuskillLatest(){
        List<SkuSkillRedisTo> redisTos = seckillService.querySkuskillLatest();
        return R.ok().put("data",redisTos);
    }

    @GetMapping("/seckill/sku/{skuId}")
    @ResponseBody
    public R seckillSku(@PathVariable("skuId")Long skuId){
        SkuSkillRedisTo redisTo = seckillService.seckillSku(skuId);
        return R.ok().setData(redisTo);
    }
}
